<?php
load()->func('tpl');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$_W['page']['title'] = '评价参数设置';
if ($operation == 'display') {
	pdo_query("CREATE TABLE IF NOT EXISTS `ims_tg_evaluateDetail` (
	  `id` int(11) NOT NULL AUTO_INCREMENT,
	  `uniacid` int(11) DEFAULT '0',
	  `jishiid` int(11) DEFAULT '0',
	  `gid` int(11) DEFAULT '0',
	  `cartid` int(11) DEFAULT '0',
	  `name` varchar(50) DEFAULT '',
	  `weight` float(10,2) DEFAULT '0.00',
	  `score` float(10,2) DEFAULT '0.00',
	  PRIMARY KEY (`id`),
	  KEY `idx_uniacid` (`uniacid`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8;
");

	$list = pdo_fetchall("SELECT * FROM " . tablename('tg_evaluate') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id DESC");

	if (checksubmit('submit')) {
		$titleLen=count($_POST['param_title']);
		$comValue=0.00;
		for ($k=0;$k<$titleLen;$k++) {
			$comValue+=$_POST['param_value'][$k];
		}
		if($comValue!=1){
			echo "<script>alert('权重总和不等于1')</script>";
		}else{
			pdo_delete('tg_evaluate', array('uniacid' =>$_W['uniacid']));

			for ($k=0;$k<$titleLen;$k++) {
				$data = array(
					'uniacid' => $_W['uniacid'],
					'name' => $_POST['param_title'][$k],
					'weight' =>$_POST['param_value'][$k]
				);
				pdo_insert('tg_evaluate', $data);
			}
			message('更新成功！', web_url('order/evaluate', array('op' => 'display')), 'success');
		}
    }

	include wl_template('order/evaluate');
} elseif ($operation == 'post') {
	$id = intval($_GPC['id']);
	if (checksubmit('submit')) {
		$data = array(
			'uniacid' => $_W['uniacid'],
			'advname' => $_GPC['advname'],
			'link' => $_GPC['link'],
			'enabled' => intval($_GPC['enabled']),
			'displayorder' => intval($_GPC['displayorder']),
			'thumb'=>$_GPC['thumb']
		);
		if (!empty($id)) {
			pdo_update('tg_adv', $data, array('id' => $id));
		} else {
			pdo_insert('tg_adv', $data);
			$id = pdo_insertid();
		}
		message('更新幻灯片成功！', web_url('store/adv', array('op' => 'display')), 'success');
	}
	$adv = pdo_fetch("select * from " . tablename('tg_adv') . " where id=:id and uniacid=:uniacid limit 1", array(":id" => $id, ":uniacid" => $_W['uniacid']));
	include wl_template('store/adv');
} elseif ($operation == 'delete') {
	$id = intval($_GPC['id']);
	$adv = pdo_fetch("SELECT id FROM " . tablename('tg_adv') . " WHERE id = '$id' AND uniacid=" . $_W['uniacid'] . "");
	if (empty($adv)) {
		message('抱歉，幻灯片不存在或是已经被删除！', web_url('store/adv', array('op' => 'display')), 'error');
	}
	pdo_delete('tg_adv', array('id' => $id));
	message('幻灯片删除成功！', web_url('store/adv', array('op' => 'display')), 'success');
} else {
	message('请求方式不存在');
}exit();